

package com.loong.loong.hik.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loong.loong.common.core.util.R;
import com.loong.loong.common.excel.annotation.ResponseExcel;
import com.loong.loong.common.log.annotation.SysLog;
import com.loong.loong.hik.entity.Monitor;
import com.loong.loong.hik.service.MonitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 监控表
 *
 * @author jinyiang
 * @date 2023-03-29 11:06:38
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/monitor" )
@Api(value = "monitor", tags = "监控表管理")
public class MonitorController {

    private final  MonitorService monitorService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param monitor 监控表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('hik_monitor_view')" )
    public R getMonitorPage(Page page, Monitor monitor) {
		return R.ok(monitorService.page(page,
				Wrappers.query(monitor).lambda()
						.like(StringUtils.isNotEmpty(monitor.getLikeName()), Monitor::getCameraName,monitor.getCameraName())
						.between(StringUtils.isNotEmpty(monitor.getStartTime())&&StringUtils.isNotEmpty(monitor.getEndTime()),Monitor::getCreateTime,monitor.getStartTime(),monitor.getEndTime())));
    }


    /**
     * 通过id查询监控表
     * @param monitorId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{monitorId}" )
    @PreAuthorize("@pms.hasPermission('hik_monitor_view')" )
    public R getById(@PathVariable("monitorId" ) Integer monitorId) {
        return R.ok(monitorService.getById(monitorId));
    }

    /**
     * 新增监控表
     * @param monitor 监控表
     * @return R
     */
    @ApiOperation(value = "新增监控表", notes = "新增监控表")
    @SysLog("新增监控表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('hik_monitor_add')" )
    public R save(@RequestBody Monitor monitor) {
        return R.ok(monitorService.save(monitor));
    }

    /**
     * 修改监控表
     * @param monitor 监控表
     * @return R
     */
    @ApiOperation(value = "修改监控表", notes = "修改监控表")
    @SysLog("修改监控表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('hik_monitor_edit')" )
    public R updateById(@RequestBody Monitor monitor) {
        return R.ok(monitorService.updateById(monitor));
    }

    /**
     * 通过id删除监控表
     * @param monitorId id
     * @return R
     */
    @ApiOperation(value = "通过id删除监控表", notes = "通过id删除监控表")
    @SysLog("通过id删除监控表" )
    @DeleteMapping("/{monitorId}" )
    @PreAuthorize("@pms.hasPermission('hik_monitor_del')" )
    public R removeById(@PathVariable Integer monitorId) {
        return R.ok(monitorService.removeById(monitorId));
    }


    /**
     * 导出excel 表格
     * @param monitor 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hik_monitor_export')" )
    public List<Monitor> export(Monitor monitor) {
		List<Monitor> list = monitorService.list(Wrappers.query(monitor));

		return monitorService.list(Wrappers.query(monitor));
    }


	/**
	 * 查询监控点list
	 * @param monitor 监控表
	 * @return
	 */
	@GetMapping("/list" )
	public R getMonitorPage(Monitor monitor) {
		return R.ok(monitorService.list(
				Wrappers.query(monitor).lambda()
						.like(StringUtils.isNotEmpty(monitor.getLikeName()), Monitor::getCameraName,monitor.getCameraName())
						.between(StringUtils.isNotEmpty(monitor.getStartTime())&&StringUtils.isNotEmpty(monitor.getEndTime()),Monitor::getCreateTime,monitor.getStartTime(),monitor.getEndTime())));
	}
}
